package com.ph.irp.base.accountbook.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.accountbook.service.AccountbookService;
import com.ph.irp.dto.AccountbookDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class AccountbookAction extends ActionSupport implements ShowPageList{
	// 视图定位字符
	private String FORWORD = null;
	// AccountbookService 用于查询，更新数据库中会计科目的信息
	private AccountbookService accountService = null;

	// 从service中查询得到的List<Accountbook>类型数组链表
	private List<AccountbookDTO> accountbookList = null;
	
	private List<AccountbookDTO> tempAccountbookList = null;

	// 会计科目
	private PBaseAccountbook pbaseAccountbook = null;

	// 存储选中复选框专递的会计科目id
	private Object[] rowlist = null;

	// 处理前台页面中的checkbox，前台的值拼接成逗号相隔的字符串
	private String hidden = null;
	private String cpage;
	private PageDTO page;

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}
	
	/**
	 * 查询会计科目
	 * 
	 * @return
	 */
	public String showAccountbooks() throws Throwable {
		tempAccountbookList = accountService.getAccountbooks(pbaseAccountbook);
		page = PageUtil.generatePageDTO(tempAccountbookList,
				"showAccountbooksPageList.action", cpage);
		accountbookList = page.getResList();
		pbaseAccountbook = null;

		FORWORD = "success";
		return FORWORD;
	}
	
	public String showPageList() throws Throwable {
		if (tempAccountbookList != null) {
			page = PageUtil.generatePageDTO(tempAccountbookList,
					"showAccountbooksPageList.action", cpage);
			accountbookList = page.getResList();
			FORWORD = "success";
		} else {
			FORWORD = "input";
		}
		return FORWORD;
	}

	/**
	 * 新增会计科目
	 * 
	 * @param pbaseAccountbook
	 * @return 放回字符串定位视图
	 */
	public String addAccountbook() throws Throwable {

		// 将前台传过来的checkbox值赋给会计科目的变量
		String[] hid = hidden.split(",");
		pbaseAccountbook.setEndExchange(Integer.valueOf(hid[0]));
		pbaseAccountbook.setDealingCheck(Integer.valueOf(hid[1]));
		pbaseAccountbook.setAmountCashCheck(Integer.valueOf(hid[2]));
		pbaseAccountbook.setCashAccountBook(Integer.valueOf(hid[3]));
		pbaseAccountbook.setBankAccountBook(Integer.valueOf(hid[4]));
		pbaseAccountbook.setOutDate(Integer.valueOf(hid[5]));

		pbaseAccountbook.setCashEquivalent(Integer.valueOf(hid[6]));
		// 新增会计科目
		accountService.addAccountbook(pbaseAccountbook);

		FORWORD = "success";

		pbaseAccountbook = null;

		return FORWORD;
	}

	public String editAccountbook() throws Throwable {

		// 将前台传过来的checkbox值赋给会计科目的变量
		String[] hid = hidden.split(",");
		pbaseAccountbook.setEndExchange(Integer.valueOf(hid[0]));
		pbaseAccountbook.setDealingCheck(Integer.valueOf(hid[1]));
		pbaseAccountbook.setAmountCashCheck(Integer.valueOf(hid[2]));
		pbaseAccountbook.setCashAccountBook(Integer.valueOf(hid[3]));
		pbaseAccountbook.setBankAccountBook(Integer.valueOf(hid[4]));
		pbaseAccountbook.setOutDate(Integer.valueOf(hid[5]));
		pbaseAccountbook.setCashEquivalent(Integer.valueOf(hid[6]));

		// 修改会计科目
		accountService.updateAccountbook(pbaseAccountbook);
		FORWORD = "success";

		pbaseAccountbook = null;
		return FORWORD;
	}
	
	

	/**
	 * 跳转到新增页面
	 * 
	 * @return
	 */
	public String addInputAccountbook() throws Throwable {
		// System.out.println("oooooooooooooooooooooo");
		pbaseAccountbook = null;
		hidden = null;

		FORWORD = "success";
		return FORWORD;

	}

	/**
	 * 删除会计科目
	 * 
	 * @return
	 */
	public String deleteAccountbook() throws Throwable {
		int i = 0;
		for (i = 0; i < rowlist.length; i++) {
			Integer id = Integer.valueOf((String) rowlist[i]);
			accountService.deleteAccountbookById(id);
		}
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 跳转到会计科目修改页面
	 * 
	 * @return
	 */
	public String updateAccountbook() throws Throwable {

		hidden = "";
		// 得到要处理的会计科目id
		Integer accountbookId = Integer.valueOf((String) rowlist[0]);

		pbaseAccountbook = accountService.getAccountbookById(accountbookId);

		hidden = hidden + pbaseAccountbook.getEndExchange() + ",";
		hidden = hidden + pbaseAccountbook.getDealingCheck() + ",";
		hidden = hidden + pbaseAccountbook.getAmountCashCheck() + ",";
		hidden = hidden + pbaseAccountbook.getCashAccountBook() + ",";
		hidden = hidden + pbaseAccountbook.getBankAccountBook() + ",";
		hidden = hidden + pbaseAccountbook.getOutDate() + ",";
		hidden = hidden + pbaseAccountbook.getCashEquivalent() + ",";
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 该方法用于在选择页面上得到会计科目的id和name
	 * 
	 * @return 返回字符串定位视图
	 */
	public String selectAccountbook() throws Throwable {		
		tempAccountbookList = accountService.getAccountbooks(pbaseAccountbook);
		page = PageUtil.generatePageDTO(tempAccountbookList,
				"showAccountbooksPageList.action", cpage);
		accountbookList = page.getResList();
		pbaseAccountbook = null;
		// 定位视图
		FORWORD = "success";

		return FORWORD;
	}

	/**
	 * selectMulti用于一个页面上有多个会计科目引用的情况，只要name值
	 * 
	 * @return 返回字符串定位相关的视图
	 */
	public String selectMultiAccountbook() throws Throwable {
		tempAccountbookList = accountService.getAccountbooks(pbaseAccountbook);
		page = PageUtil.generatePageDTO(tempAccountbookList,
				"showAccountbooksPageList.action", cpage);
		accountbookList = page.getResList();
		pbaseAccountbook = null;
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * action的默认方法
	 */
	public String execute() throws Exception {

		try {
			return this.showAccountbooks();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}

	}

	/**
	 * getters and setters for instance parameters
	 * 
	 * @return
	 */

	public AccountbookService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountbookService accountService) {
		this.accountService = accountService;
	}

	public List<AccountbookDTO> getAccountbookList() {
		return accountbookList;
	}

	public void setAccountbookList(List<AccountbookDTO> accountbookList) {
		this.accountbookList = accountbookList;
	}

	public PBaseAccountbook getPbaseAccountbook() {
		return pbaseAccountbook;
	}

	public void setPbaseAccountbook(PBaseAccountbook pbaseAccountbook) {

		this.pbaseAccountbook = pbaseAccountbook;

	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String getHidden() {
		return hidden;
	}

	public void setHidden(String hidden) {
		this.hidden = hidden;
	}

}
